<template>
  <dictionaryManageView
    :isLoading="isLoading"
    :addDicdialog="addDicdialog"
    :tableData="tableData"
    :total="total"
    :pageSize="pageSize"
    :currentPage="currentPage"
    :formLabelAlign="formLabelAlign"
    @changePage="changePage"
    @sizeChange="sizeChange"
    @addDictionary="addDictionary"
    @addCancel="addCancel"
    @addKeep="addKeep"
    @delDic="delDic"
    @addDicClose="addDicClose"
    @findDictionary="findDictionary"
    ref="dicManage"
  >

  </dictionaryManageView>
</template>

<script>
  import dictionaryManageView from './dictionaryManageView'

  export default {

    components: {
      dictionaryManageView
      // name
    },
    name: "dictionaryManageLogic",
    props: {
      // msg: String //  [name: typeof]
    },
    filters: {},
    computed: {},
    watch: {},
    data() {
      return {
        isLoading: false, // 加载状态
        total: -1, // 数据总条数
        pageSize: 10, // 每页条数
        tableDataSum: [], // 所有数据
        tableData: [], // 当前页所需要的数据
        currentPage: 1, // 当前页
        formLabelAlign: {
          datacode: "",
          datavalue: "",
          datatype: "",
          datadesc: "",
          sortno: ""
        },
        addDicdialog: false // 弹窗状态
      }
    },
    created() {
      this.getDictList();
    },
    mounted() {

    },
    methods: {
      getDictList(obj) {
        this.isLoading = true;
        _https.post.getDict(obj).then(res => {
          if (res.data.code == 0) {
            let data = res.data.data;
            this.tableDataSum = res.data.data;
            let currentPage = this.currentPage;
            if (data.length % this.pageSize == 0 && currentPage != 1) {
              currentPage = Math.ceil(data.length / this.pageSize);
              this.currentPage = currentPage;
            }
            this.setDataList(currentPage);
            this.total = data.length;
            this.isLoading = false;
          } else {
            this.$message({type: 'error', message: res.data.message});
          }
        })
      },
      findDictionary(dictionarySeach) {
        this.currentPage = 1;
        let obj = {
          datavalue: dictionarySeach,
          // datatype: dictionarySeach,
          // datacode: dictionarySeach
        };
        this.getDictList(obj);
      },
      /**
       * 将返回的数据进行分页操作
       * @param currentPage {Number}
       */
      setDataList(currentPage) {
        let current = currentPage * this.pageSize;
        let currentStart = current - this.pageSize;
        let currentEnd = current;
        this.tableData = this.tableDataSum.slice(currentStart, currentEnd);
      },
      /**
       * 分页改变当前页触发
       * @param page 返回值参数（当前页）
       */
      changePage(page) {
        this.currentPage = page;
        this.setDataList(page);
        this.reset();
      },
      /**
       * 改变每页条数的回调
       * @param num 每页条数
       */
      sizeChange(num) {
        this.pageSize = num;
        this.getDictList();
      },
      // 关闭弹窗的回调
      addDicClose() {
        this.addDicdialog = false;
        this.$refs.dicManage.$refs.addDicform.resetFields();
        this.reset();
      },
      // 新增参数
      addDictionary() {
        this.addDicdialog = true;
      },
      // 取消新增
      addCancel() {
        this.addDicdialog = false;
        this.reset();
      },
      // 保存新增
      addKeep() {
        this.$refs.dicManage.$refs.addDicform.validate((valid) => {
          if (!valid) {
            return;
          } else {
            let newObj = {...this.formLabelAlign};
            _https.post.getSaveDict(newObj).then(res => {
              if (res.data.code == 0) {
                this.$message({type: 'success', message: res.data.message});
                this.getDictList();
                this.addDicdialog = false;
              } else {
                this.$message({type: 'error', message: res.data.message});
              }
            })

          }
        })
      },
      delDic(index, row) {
        let obj = {
          id: row.id
        };
        this.$confirm('此操作将永久删除该参数, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _https.post.getDeleteDict(obj).then(res => {
            if (res.status == 200) {
              this.$message({type: 'success', message: res.data.message});
              this.reset();
              this.getDictList();
            }else {
              this.$message({type: 'error', message: res.data.message});
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      reset() {
        this.formLabelAlign = {datadesc: ""}
      }
    },
    destroyed() {

    }
  }
</script>

<style scoped lang="less">

</style>
